package com.zangchat.utils;

import java.util.Map;
import java.util.Random;

/**
 * Created by GaoTing on 2019/4/28
 * Explain:自定义工具类
 */
public class Tools {
    /**
     * 判断是否为空字符串
     * 纯空格也被认为是空符串
     *
     * @param string
     * @return
     */
    public static boolean isNullString(String string) {
        return string == null || string.trim().length() == 0;
    }

    /**
     * 验证是否为手机号
     * 验证规则:1开头 11位纯数字;
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        String regex = "^1\\d{10}$";
        return !isNullString(phone) || phone.matches(regex);
    }

    /**
     * 判断是否符合密码规则
     * 密码规则:数字,小写字母,大写字母
     *
     * @param password
     * @return
     */
    public static boolean passwordRegex(String password) {
        String regex = "^[a-z0-9A-Z]{6,16}+$";
        return password.matches(regex);
    }

    /**
     * 判断是否符合支付密码规则
     * 密码规则:6位数字
     *
     * @param password
     * @return
     */
    public static boolean payPasswordRegex(String password) {
        String regex = "^[0-9]{6}$";
        return password.matches(regex);
    }

    /**
     *  获取数字随机数
     * @param length  随机数位数
     * @return
     */
    public static int getRandomNum(int length) {
        Random random = new Random();
        int  pow = (int)Math.pow(Double.parseDouble("10"), Double.parseDouble(length + ""));
        String  fourRandom = random.nextInt(Integer.parseInt(pow+"")) +"";
        int randLength = fourRandom.length();
        if(randLength<length){
            for(int i=1; i<=length-randLength; i++)
                fourRandom = "0" + fourRandom;
        }
        return Integer.parseInt(fourRandom);



//        int ranStr = 0;
//        ranStr += (new Random().nextInt(9) + 1);
//        for (int i = 0; i < length - 1; i++) {
//            int x = new Random().nextInt(10);
//            ranStr += x;
//        }
//        return ranStr;

    }


    /**
     * 获取随机字符串(字母和数字)
     */
    public static String getRandomString(int length) {
        String string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String ranStr = "";
        for (int i = 0; i < length; i++) {
            int x = new Random().nextInt(string.length());
            ranStr += string.substring(x, x + 1);
        }
        return ranStr;
    }

    /**
     * 生成盐值
     *
     * @return
     */
    public static int getSalt() {
        return getRandomNum(4);
    }
    /**
     * 判断double是否是整数
     * @param obj
     * @return
     */
    public static boolean isIntegerForDouble(double obj) {
        double eps = 1e-10;  // 精度范围
        return obj-Math.floor(obj) < eps;
    }

}
